package com.eastfair.builder.service.impl;

import com.eastfair.builder.service.BuilderHomeServiceOrderService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.api.HomeServiceOrderFeign;
import com.eastfair.venueservice.enumeration.ServiceOrderOrderStatusEnum;
import com.eastfair.venueservice.enumeration.ServiceOrderPayStatusEnum;
import com.eastfair.venueservice.vo.ServiceOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 主场服务订单服务
 *
 * @author dqq
 * @date 2023 /6/4
 */
@Service
@Slf4j
public class BuilderHomeServiceOrderServiceImpl implements BuilderHomeServiceOrderService {

    @Resource
    private HomeServiceOrderFeign homeServiceOrderFeign;

    @Override
    public List<ServiceOrderVO> queryManageOrderByExhibitorIdList(List<Long> exhibitorIdList) {
        log.info("queryManageOrderByExhibitorIdList - 展商ID查询保证金订单, exhibitorIdList={}", exhibitorIdList);
        R<List<ServiceOrderVO>> result = homeServiceOrderFeign.queryManageOrderByExhibitorIdList(exhibitorIdList);
        log.info("queryManageOrderByExhibitorIdList - 展商ID查询保证金订单, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public Map<Long, String> getManageOrderPayStatus(List<Long> exhibitorIdList) {
        List<ServiceOrderVO> manageOrderList = queryManageOrderByExhibitorIdList(exhibitorIdList);
        if (manageOrderList == null || manageOrderList.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<Long, List<ServiceOrderVO>> manageOrderMap = manageOrderList
                .stream()
                .collect(Collectors.groupingBy(ServiceOrderVO::getExhibitorId));
        Map<Long, String> manageOrderPayStatusMap = new HashMap<>(16);
        for (Map.Entry<Long, List<ServiceOrderVO>> entry : manageOrderMap.entrySet()) {
            Long exhibitorId = entry.getKey();
            List<ServiceOrderVO> childManageOrderList = entry.getValue();
            // 还未生成管理费订单，设为未支付
            if (childManageOrderList == null || childManageOrderList.isEmpty()) {
                manageOrderPayStatusMap.put(exhibitorId, ServiceOrderPayStatusEnum.NOT_PAY.getValue());
                continue;
            }
            boolean existHasPay = false;
            boolean existHasCancel = false;
            for (ServiceOrderVO manageOrderVO: childManageOrderList) {
                if (ServiceOrderPayStatusEnum.HAS_PAY.equals(manageOrderVO.getPayStatus())) {
                    existHasPay = true;
                }
                if (ServiceOrderOrderStatusEnum.CANCEL.eq(manageOrderVO.getOrderStatus())) {
                    existHasCancel = true;
                }
            }
            // 如果有被主场取消的，或者是被支付的，则认为已缴纳
            if (existHasPay || existHasCancel) {
                manageOrderPayStatusMap.put(exhibitorId, ServiceOrderPayStatusEnum.HAS_PAY.getValue());
            } else {
                manageOrderPayStatusMap.put(exhibitorId, ServiceOrderPayStatusEnum.NOT_PAY.getValue());
            }
        }
        return manageOrderPayStatusMap;
    }

    @Override
    public List<ServiceOrderVO> queryOrderByExhibitorId(Long exhibitorId) {
        log.info("queryOrderByExhibitorId - 展商展位ID查询订单列表, exhibitorId-{}", exhibitorId);
        R<List<ServiceOrderVO>> result = homeServiceOrderFeign.queryOrderByExhibitorId(exhibitorId);
        log.info("queryOrderByExhibitorId - 展商展位ID查询订单列表, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

}